<?php
class ControllerLocalisationZone extends AdminController {

	public function index() {
		M('localisation/zone');

		$this->getList();
	}

	public function insert() {
		M('localisation/zone');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_zone->addZone($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/zone'));
		}

		$this->getForm();
	}

	public function update() {
		M('localisation/zone');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_zone->editZone($this->request->get['zone_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/zone'));
		}

		$this->getForm();
	}

	public function delete() {
		M('localisation/zone');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $zone_id) {
				$this->model_localisation_zone->deleteZone($zone_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/zone'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$zone_total = $this->model_localisation_zone->getTotalZones();
		$this->data['zones'] = $this->model_localisation_zone->getZones($qf);

		$this->data['pagination'] = $this->pagination($zone_total)->render();

		$this->display('localisation/zone_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		$zone_id = G('zone_id', 0);
		if ($zone_id) {
			$this->data['action'] = UA('localisation/zone/update', 'zone_id=' . $zone_id);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_zone->getZone($zone_id);
			}
		} else {
			$this->data['action'] = UA('localisation/zone/insert');
		}
		
		$this->data['countries'] = cache_read('country.php');

		$this->display('localisation/zone_form.tpl');
	}

	protected function validateForm() {

		if (!range_length($this->request->post['name'], 3, 64)) {
			$this->setMessage('error_name', $this->_['error_name']);
			return false;
		}

		return true;
	}

	protected function validateDelete() {

		M('setting/store');
		M('sale/customer');
		M('sale/affiliate');
		M('localisation/geo_zone');

		foreach ($this->request->post['selected'] as $zone_id) {
			if (C('config_zone_id') == $zone_id) {
				$this->setMessage('error_warning', $this->_['error_default']);
				return false;
			}

			$store_total = $this->model_setting_store->getTotalStoresByZoneId($zone_id);

			if ($store_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
				return false;
			}

			$address_total = $this->model_sale_customer->getTotalAddressesByZoneId($zone_id);

			if ($address_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_address'], $address_total));
				return false;
			}

			$affiliate_total = $this->model_sale_affiliate->getTotalAffiliatesByZoneId($zone_id);

			if ($affiliate_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_affiliate'], $affiliate_total));
				return false;
			}

			$zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByZoneId($zone_id);

			if ($zone_to_geo_zone_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_zone_to_geo_zone'], $zone_to_geo_zone_total));
				return false;
			}
		}
		return true;
	}
}
?>